-
-
Notifications
You must be signed in to change notification settings - Fork 360
Implementation Edit to Bogo Sort in Javascript #291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation Edit to Bogo Sort in Javascript #291
Conversation
- Added main() - bogoSort() now returns the sorted array
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JS version was missing a main
function. Good catch. But I can't merge this in its current state.
@@ -12,6 +12,7 @@ function bogoSort(arr) { | |||
while (!isSorted(arr)) { | |||
shuffle(arr); | |||
} | |||
return arr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function doesn't have to return the array, because it's passed by reference anyway.
|
||
function main() { | ||
var testArray = [4,5,123,24,34,-5]; | ||
print(bogoSort(testArray)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print
does not exist in JavaScript. And because you don't need return arr
above, you'd have to output it like this:
bogoSort(testArray);
console.log(testArray);
@@ -22,3 +23,10 @@ function shuffle(arr) { | |||
arr[r] = tmp; | |||
} | |||
} | |||
|
|||
function main() { | |||
var testArray = [4,5,123,24,34,-5]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly to the other PR: JS files should be indented with 2 spaces and use const for non-chaning variables (and let for mutable ones). var is pretty much legacy at this point. Also, there should be spaces after the commas in the array initialization ([1, 2, 3] instead of [1,2,3]).
Sorry for all the mistakes in my Javascript PRs. Clearly I'm not super comfortable with Javascript. Thanks for helping me out with my PRs and I think I've fixed both Javascript PRs. |
@@ -25,8 +24,9 @@ function shuffle(arr) { | |||
} | |||
|
|||
function main() { | |||
var testArray = [4,5,123,24,34,-5]; | |||
print(bogoSort(testArray)); | |||
let testArray = [4, 5, 123, 24, 34, -5]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't change, it should be const
. Same in the other PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, sorry about that. I made a commit to correct the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this not change? It's being sorted later.
@Butt4cak3 @Gustorn How is the state of this PR? |
Looks fine with changes requested |
@@ -22,3 +22,11 @@ function shuffle(arr) { | |||
arr[r] = tmp; | |||
} | |||
} | |||
|
|||
function main() { | |||
const testArray = [4, 5, 123, 24, 34, -5]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know JS too well, but if you declare testArray
and const
can you actually modify it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, you can not reassign this. but you can add/remove/reorder elements in array like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's pretty silly ^^
Changes were made as requested, Butt4cak3 was away
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes were made as requested, thank you!
Added main() to code so that it can be run as is